using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuotesPlugin
{
    public partial class SelectQuotesForm : Form
    {
        public SelectQuotesForm()
        {
            InitializeComponent();
            this.checkedListBox1.SelectionChanged += new CheckedListBoxWithImage.SelectionChangedHandler(checkedListBox1_SelectionChanged);
        }

        
        private void SelectQuotesForm_Load(object sender, EventArgs e)
        {
            try
            {
                // load quotes from RSS, websites, etc.
                IQuotesProvider reader = new Quotes4AllProvider();
                for (int i = 0; i < reader.QuotesCount; i++)
                {
                    IQuotesEntry quote = reader.GetQuote(i);

                    this.checkedListBox1.Add(quote);
                }
            }
            catch (Exception ex)
            {
                ReportException(ex);
            }
        }

        private void insertButton_Click(object sender, EventArgs e)
        {
            foreach (int idx in this.checkedListBox1.CheckedIndices)
            {
                if (this.checkedListBox1.GetItemChecked(idx))
                {
                    this.content += this.checkedListBox1.Controls[idx];
                }
            }

            if (this.checkedListBox1.CheckedIndices.GetLength(0) > 0)
            {   
                string copyright = String.Format("[Powered by <a href=\"{0}\">QuotesPlugin</a> v{1} for Windows Live Writer]", Plugin.PublisherUrl, Plugin.Version);
                this.content += copyright;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        public string Content
        {
            get { return this.content; }
        }

        private void ReportException(Exception ex)
        {
            this.labelStatus.Text = "ERROR: " + ex.Message;
        }

        void checkedListBox1_SelectionChanged(object sender, CheckedListBoxWithImage.SelectionChangedEventArgs e)
        {
            if (e.Change == CheckedListBoxWithImage.SelectionChangedEventArgs.ChangeType.Add)
            {
                m_selectedCount++;
            }
            else
            {
                m_selectedCount--;
            }

            this.insertButton.Enabled = (m_selectedCount != 0);
        }

        private string content = "";

        private int m_selectedCount = 0;
    }
}
